package xworker.gluon.glisten.application;

import com.gluonhq.charm.glisten.application.AppManager;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.javafx.event.SimpleThingEventHandler;

public class AppManagerActions {
    public static Object switchView(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        String viewName = self.doAction("getViewName", actionContext);
        if(viewName != null){
            return AppManager.getInstance().switchView(viewName);
        }
        return null;
    }

    public static Object retrieveView(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        String viewName = self.doAction("getViewName", actionContext);
        if(viewName != null){
            return AppManager.getInstance().retrieveView(viewName);
        }

        return null;
    }

    public static boolean removeViewFactory(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        String viewName = self.doAction("getViewName", actionContext);
        if(viewName != null){
            return AppManager.getInstance().removeViewFactory(viewName);
        }
        return false;
    }

    public static void hideAllLayers(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        Boolean hideAllLayers = self.doAction("isHideAllLayers", actionContext);
        AppManager.getInstance().hideAllLayers(hideAllLayers);
    }

    public static boolean removeLayerFactory(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        String layerName = self.doAction("getLayerName", actionContext);
        if(layerName != null){
            return AppManager.getInstance().removeLayerFactory(layerName);
        }
        return false;
    }

    public static void hideLayer(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        String layerName = self.doAction("getLayerName", actionContext);
        if(layerName != null){
            AppManager.getInstance().hideLayer(layerName);
        }
    }

    public static void showLayer(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        String layerName = self.doAction("getLayerName", actionContext);
        if(layerName != null){
            AppManager.getInstance().showLayer(layerName);
        }
    }

    public static void showMessage(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        String message = self.doAction("getMessage", actionContext);
        String buttonText = self.doAction("getButtonText", actionContext);

        AppManager.getInstance().showMessage(message, buttonText,  new SimpleThingEventHandler<>(self, actionContext));
    }
}
